﻿@model MalkiSumWeb.Models.LogOnModel
@{
    ViewBag.Title = "Login / Signup";
}
<h2>
    @ViewBag.Message</h2>
<!-- normal form fileds start -->
<div class="form">
    <h3>
        <i class="fa fa-sign-in"></i>Login</h3>
    <form class="form-horizontal" role="form">
    <div class="form-group">
        <label for="inputEmail" class="col-sm-2 control-label">
            Email</label>
        <div class="col-sm-5">
            <input id="signin-email" type="text" class="form-control" placeholder="someone@mail.com"
                onkeydown="testDown(this)" onkeypress="testPress(this)" onkeyup="testUp(this)" />
            <span id="invalid-signin-email" style="display: none; color: Red;">Not a valid email
                address</span>
            <input id="hdsignin" type="hidden" value="0" />
        </div>
    </div>
    <div class="form-group">
        <label for="inputPassword" class="col-sm-2 control-label">
            Password</label>
        <div class="col-sm-5">
            <input id="signin-password" type="password" class="form-control" placeholder="Password">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-5">
            <a class="btn btn-link" onclick="ResendPassword()">Forgot password?</a>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-5">
            <div class="checkbox">
                <label>
                    <input type="checkbox">
                    Remember me
                </label>
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="button" class="btn btn-primary" onclick="SignIn()">
                Login</button>
        </div>
    </div>
    </form>
</div>
<div class="form">
    <h3>
        <i class="fa fa-sign-in"></i>Signup</h3>
    <form class="form-horizontal" role="form">
    <div class="form-group has-success">
        <label class="control-label col-sm-2" for="inputFName">
            Name</label>
        <div class="col-sm-5">
            <input id="signup-user-name" type="text" class="form-control" id="inputFName" placeholder="User Name" />
        </div>
    </div>
    <div class="form-group">
        <label for="inputEmail" class="col-sm-2 control-label">
            Email</label>
        <div class="col-sm-5">
            <input id="signup-user-email" type="text" class="form-control" placeholder="someone@mail.com" />
            <span id="invalid-signup-user-email" style="display: none; color: Red;">Not a valid
                email address</span>
            <input id="hdsignup" type="hidden" value="0" />
        </div>
        Email will be used as a login name.
    </div>
    <div class="form-group">
        <label for="inputPassword" class="col-sm-2 control-label">
            Password</label>
        <div class="col-sm-5">
            <input id="signup-user-password" type="password" class="form-control" placeholder="Password">
            <span class="help-block">Password Can't contain part of the username &amp; must have
                numeric, symbol &amp; letters.</span>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="button" class="btn btn-primary" onclick="SignUp()">
                Signup</button>
        </div>
    </div>
    </form>
</div>
<!-- normal form fileds end -->
<script type="text/javascript">

    $(document).ready(function() {
        setTimeout(ValidateEmailAndPassword, 1000);
    });
    
    function ValidateEmailAndPassword () {
        $('#signin-email').blur(validateSignInEmail());
        $('#signup-user-email').blur(validateSignUpEmail());
    };


    function validateSignInEmail() {
        var email = $('#signin-email').val();
        var re = /^(([^<>()[\]\\.,;:\s@@\"]+(\.[^<>()[\]\\.,;:\s@@\"]+)*)|(\".+\"))@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/igm;
        if (re.test(email)) {
            $('#invalid-signin-email').hide();
            $('#hdsignin').val("1");
        } else {
            $('#invalid-signin-email').show();
            $('#hdsignin').val("0");
        }
    }



    function validateSignUpEmail() {
        var email = $('#signup-user-email').val();
        var re = /^(([^<>()[\]\\.,;:\s@@\"]+(\.[^<>()[\]\\.,;:\s@@\"]+)*)|(\".+\"))@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/igm;
        if (re.test(email)) {
            $('#invalid-signup-user-email').hide();
            $('#hdsignup').val("1");
        } else {
            $('#invalid-signup-user-email').show();
            $('#hdsignup').val("0");
        }
    }

    function testDown(t) {
        var code = (event.keyCode ? event.keyCode : event.which);
        if (t.value == t.defaultValue) {
            t.value = ''; t.style.color = 'black';
        }
        if (code == 32) {
            event.returnValue = false;
            return false;
        }

        if (t.value.length == 0 && code >= 48 && code <= 57) {
            event.returnValue = false;
            return false;
        }
    }
    function testUp(t) {
        var code = (event.keyCode ? event.keyCode : event.which);
        //alert(t.value.length + ' ' + t.value);
        if (t.value.length == 1 && code >= 48 && code <= 57) {
            t.value = '';
            event.returnValue = false;
            return false;
        }
    }
    function testPress(t) {
        //            var code = (event.keyCode ? event.keyCode : event.which);
        //            
        //            if (code >= 48 && code <= 57) {
        //               
        //                event.returnValue = false;
        //                return false;
        //            }
    }
    //alert(this.value.length+' '+ this.value);
</script>
<script type="text/javascript">
    function SignIn() {

        var userName = $("#signin-email").val();
        var password = $("#signin-password").val();
        if (userName.trim() == '' || password.trim() == '') {
            alert(' اكتب الايميل وكلمة المرور أولا');
            return;
        }

        validateSignInEmail();
        if ($('#hdsignin').val() != "0") {
            $.post('@Url.Action("SignIn", "Account")', { UserName: userName.trim(), Password: password.trim() }, function (ret) {
                if (ret.success) {
                    window.location.href = '@Url.Action("Index", "Home")';
                    //window.location.href = '@Url.Action("Profile", "Account")';
                }
                else {
                    alert(ret.msg);
                }
            });
        }
        else {
            alert("Invalid email, can not login.");
        }
    }

    function SignUp() {
        var userName = $("#signup-user-name").val();
        var userEmail = $("#signup-user-email").val();
        var password = $("#signup-user-password").val();
        if (userName.trim() == '' || password.trim() == '' || userEmail.trim() == '') {
            alert(' اكتب الاسم والايميل وكلمة المرور أولا');
            return;
        }

        validateSignUpEmail();
        if ($('#hdsignup').val() != "0") {
            $.post('@Url.Action("SignUp", "Account")', { UserFullName: userName.trim(), UserName: userEmail.trim(), Password: password.trim() }, function (ret) {
                if (ret.success) {
                    window.location.href = '@Url.Action("Index", "Home")';
                }
                else {
                    alert(ret.msg);
                }
            });
        }
        else {
            alert("Invalid email, can not sign up.");
        }
    }

    function ResendPassword() {

        var userName = $("#signin-email").val();
        if (userName.trim() == '') {
            alert(' اكتب الايميل أولا');
            return;
        }

        validateSignInEmail();
        if ($('#hdsignin').val() != "0") {
            $.post('@Url.Action("ResendPassword", "Account")', { UserName: userName.trim() }, function (ret) {
                if (ret.success) {
                    alert("تم ارسال كلمة السر إلى " + userName.trim());
                    $("#signin-email").val("");
                    //window.location.href = '@Url.Action("Index", "Home")';
                    //window.location.href = '@Url.Action("Profile", "Account")';
                }
                else {
                    alert(ret.msg);
                }
            });
        }
        else {
            alert("Invalid email.");
        }
    }
</script>
